﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using AwesomeGame.UserInput;
using Nuclex.UserInterface;
using Nuclex.Input;
using Nuclex.UserInterface.Controls.Desktop;

namespace AwesomeGame.Screens
{
    public class MainMenu : IScreen
    {
        public delegate void ScreenChangeEvent(IScreen sender, IScreen newScreen);

        public event ScreenChangeEvent ChangeScreen;

        private KeyboardHelper _keyboard;
        private SpriteFont _font;
        private GuiManager _guiManager;
        private InputManager _inputManager;
        private InputControl _playerNameTextBox;
        private InputControl _serverAddressTextBox;
        private ButtonControl _startGameButton;
        private ButtonControl _hostGameButton;
        private Texture2D _background;

        public MainMenu()
        {
            _background = Game1.ContentManager.Load<Texture2D>(@"MainMenu\Background");

            _keyboard = new KeyboardHelper(new List<Keys> { Keys.Left, Keys.Right, Keys.Up, Keys.Down, Keys.PageUp, Keys.PageDown, Keys.Space });

            _font = Game1.ContentManager.Load<SpriteFont>(@"Fonts\candc16pt");

            _inputManager = new InputManager(Game1.GameServiceContainer, Game1.GameWindow.Handle);
            _guiManager = new GuiManager(Game1.GameServiceContainer);
            Game1.GameComponents.Add(_inputManager);
            Game1.GameComponents.Add(_guiManager);

            InitializeGUI();
        }

        public void Dispose()
        {
            Game1.GameComponents.Remove(_inputManager);
            Game1.GameComponents.Remove(_guiManager);
        }

        private void InitializeGUI()
        {
            _guiManager.DrawOrder = 1000;

            Screen mainScreen = new Screen(1024, 768);
            _guiManager.Screen = mainScreen;

            _playerNameTextBox = new InputControl();
            _playerNameTextBox.Text = "Wibble";
            _playerNameTextBox.Bounds = new UniRectangle(123, 10, 150, 22);
            mainScreen.Desktop.Children.Add(_playerNameTextBox);

            _serverAddressTextBox = new InputControl();
            _serverAddressTextBox.Text = "localhost";
            _serverAddressTextBox.Bounds = new UniRectangle(173, 40, 150, 22);
            mainScreen.Desktop.Children.Add(_serverAddressTextBox);

            _startGameButton = new ButtonControl();
            _startGameButton.Text = "Join Game";
            _startGameButton.Bounds = new UniRectangle(10, 70, 150, 32);
            _startGameButton.Enabled = true;
            _startGameButton.Pressed += new EventHandler(_startGameButton_Pressed);
            mainScreen.Desktop.Children.Add(_startGameButton); 

            _hostGameButton = new ButtonControl();
            _hostGameButton.Text = "Host Game";
            _hostGameButton.Bounds = new UniRectangle(200, 70, 150, 32);
            _hostGameButton.Enabled = true;
            _hostGameButton.Pressed += new EventHandler(_hostGameButton_Pressed);

            mainScreen.Desktop.Children.Add(_hostGameButton); 
        }

        void _hostGameButton_Pressed(object sender, EventArgs e)
        {
            IScreen gameplay = new GameplayScreen(_playerNameTextBox.Text, "localhost", true);
            if (ChangeScreen != null)
                ChangeScreen(this, gameplay);
        }

        void _startGameButton_Pressed(object sender, EventArgs e)
        {
            IScreen gameplay = new GameplayScreen(_playerNameTextBox.Text, _serverAddressTextBox.Text, false);
            if (ChangeScreen != null)
                ChangeScreen(this, gameplay);
        }


        public void Update(GameTime gameTime)
        {
            _keyboard.Update(gameTime);
        }

        public void Draw(GameTime gameTime)
        {
            Game1.SpriteBatch.Draw(_background, Vector2.Zero, null, Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0.7f);

            Game1.SpriteBatch.DrawString(_font, "Your Name:", new Vector2(10, 10), Color.Black);
            Game1.SpriteBatch.DrawString(_font, "Server address:", new Vector2(10, 40), Color.Black);

        }

       
    }
}
